Pro im_process2

;This program is a consolidation of four steps in image processing
;and two steps in cleaning non-Gausian noise.
;Processing steps:
;First, the skys are assembled and median'd.
;Second, the corresponding duration darks (median) is called up..
;Third, the median sky is made into a flat field by subtracting the
;median dark and then normalized with the mean pixel value.
;Finally, the object images are sky subtracted and flat fielded.
;Product is a set of images ready to inspect and stack.
;Cleaning steps:
;First the horizontal, high noise lines are smoothed
;second, the gausian noise is plateaued to zero line-by-line
;The procedure is intended as a "pipeline" for NIR dither patterns.


;get filenames of image files to be processed
filenames = dialog_pickfile(/multiple_files, filter = '*.fits', $
                            title = 'FITS files (images) to be processed')
;how many images?
nimages = n_elements(filenames)

;get filename of sky file for subtracting from images
sky = dialog_pickfile(title = 'Median sky FITS file', filter = '*.fits')

;get filename of dark file for flat field
dark = dialog_pickfile(title = 'Dark FITS file', filter = '*.fits')

;get path where dark-subtracted files are to be written
ds_path = dialog_pickfile(title = $
   'Choose directory to write dark-subracted FITS', /directory)
;read in sky image, convert to float
sky = double(mrdfits(sky, 0, skyhead, /unsigned))

;atv,sky
;stop

;read in dark image, convert to float
dark = double(mrdfits(dark, 0, darkhead, /unsigned))

FOR i = 0, nimages-1 DO BEGIN   ;for each image file

;read in image, conver to float; get header as well
   image = double(mrdfits(filenames[i], 0, header, /unsigned))

;subtract sky image
   image_ss = image-sky

;flat field image

;print,mean(sky-dark)
;atv, (sky-dark)/mean(sky-dark)
;stop

   image_ss_ff = image_ss / ((sky-dark)/double(mean(sky-dark)))

;set line-by-line plateaus at zero

center=median(image_ss_ff[4:1019,4:1019])
sigma=stdev(image_ss_ff[4:1019,4:1019])

;print,center
;stop

    FOR k=4, 1019 DO BEGIN   ;for each row


        ;clean out the random noisy lines first

        IF $
        (stdev(image_ss_ff[447:576,k]) gt (5.0 * stdev(image_ss_ff[447:576,k+1])))$
        ;(stdev(image_ss_ff[447:576,k]) gt ((5.0 * stdev(image_ss_ff[447:576,k+1]))$
        ;or 5.0 * stdev(image_ss_ff[447:511,k+1]) or 5.0 * stdev(image_ss_ff[512:576,k+1]))) $

        THEN BEGIN
        image_ss_ff[4:1019,k]= 0.5 *((image_ss_ff[4:1019,k-1])+(image_ss_ff[4:1019,k+1]))

        print,k

        ENDIF

    ;re-plateau each row

    row_plateau= median(image_ss_ff[4:1019,k])

    image_ss_ff[4:1019,k]=image_ss_ff[4:1019,k]-row_plateau

    ENDFOR

;*****
;reverse the image

;image_ss_ff=reverse(image_ss_ff)
;*****

;strip directory path from image filename (oth element)
   file_nopath = reverse(strsplit(filenames[i], '/', /extract))
;strip off .fits suffix
   file_begin = strsplit(file_nopath[0], '.fits', /extract, /regex)
;add write path and _pcl.fits suffix

   pcl_filename = file_begin[0] + '_pcl.fits'

;write processed image and original image header to file
   print, 'WRITING TO ' + pcl_filename
   mwrfits, image_ss_ff, pcl_filename, header
ENDFOR

END
